package cn.phub.org.controller;

import cn.phub.basic.exception.MyException;
import cn.phub.basic.util.AjaxResult;
import cn.phub.basic.util.ExcelUtil;
import cn.phub.basic.util.PageList;
import cn.phub.basic.vo.ShopVo;
import cn.phub.org.domain.Shop;
import cn.phub.org.domain.ShopAuditLog;
import cn.phub.org.query.ShopQuery;
import cn.phub.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;

    /**
     * 每种状态对应的店铺数量list
     * @return
     */
    @GetMapping("/echars")
    public Map<String,Object> shopEchars(){
        List<ShopVo> shopVos = shopService.loadStateStatistics();
        List<Integer> states = new ArrayList<>();
        List<Long> nums = new ArrayList<>();
        for (ShopVo shopVo : shopVos) {
            states.add(shopVo.getState());
            nums.add(shopVo.getNum());
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("x",states);
        map.put("y",nums);
        return map;
    }

    /**
     * 导出数据
     * @param response
     */
    @GetMapping("/export/excel")
    public void exportExcel(HttpServletResponse response){
        List<Shop> shops = shopService.queryAll();
        ExcelUtil.exportExcel(shops,null,"店铺数据", Shop.class,"店铺信息.xls",response);
    }

    /**
     * 导入数据
     */
    @PostMapping("/import/excel")
    public void importExcel(@RequestPart MultipartFile file){
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
//        System.out.println("总共："+shops.size());
    }

    /**
     * 店铺人工审核通过
     * @return
     */
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog log){
        try {
            shopService.pass(log);
            return AjaxResult.me();
        } catch (MyException e) {// 自定义业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！！");
        }
    }

    /**
     * 店铺人工审核驳回
     * @return
     */
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog log){
        try {
            shopService.reject(log);
            return AjaxResult.me();
        } catch (MyException e) {// 自定义业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！！");
        }
    }



    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (MyException e) {//自定义异常MyException
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,操作失败!");
        }
    }
    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.queryById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.queryAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query)
    {
        return shopService.queryPage(query);
    }
}
